home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue24 / ntserv / DemoCl2.dpr < prev    next >
Encoding:
Text File  |  1997-07-04  |  2.3 KB  |  78 lines

  1. program DemoCl2;
  2.  
  3. Uses Windows, SysUtils, Classes;
  4.  
  5. const
  6.   BuffSize = 32768;
  7.  
  8. var
  9.   Machine: ShortString;
  10.   Option: ShortString;
  11.   MachineBuffer: array[0..255] of char;
  12.   CommandBuffer: array[0..255] of char;
  13.   DataBuffer: array[0..BuffSize-1] of char;
  14.   DataBytes: DWord;
  15.  
  16.   procedure ShowSyntaxOptions;
  17.   begin
  18.     WriteLn('DESCRIPTION');
  19.     WriteLn('        DemoCl2 is a simple client application to communicate with the DemoSv2 Service DemoService2b using pipes.');
  20.     WriteLn('        Supplied as part of the demonstration programs to accompany the NT services article in the Delphi Magazine.');
  21.     WriteLn('USAGE');
  22.     WriteLn('        DEMOCL2 MACHINE_NAME <Option>');
  23.     WriteLn('');
  24.     WriteLn('        MACHINE_NAME is the name of the machine (use . for the local machine)');
  25.     WriteLn('        Options can be one of:-');
  26.     WriteLn('          Query - do display a list of files backed up from the monitored direcotry');
  27.     WriteLn('          Reset - to reset the list of files.');
  28.   end;
  29.  
  30.   procedure ShowReturnedData;
  31.   var
  32.     Strings: TStrings;
  33.     MemStream: TStream;
  34.     I: Integer;
  35.  
  36.   begin
  37.     Strings := TStringList.Create;
  38.     try
  39.       MemStream := TMemoryStream.Create;
  40.       try
  41.         MemStream.Write(DataBuffer,DataBytes);
  42.         MemStream.Position := 0;
  43.         Strings.LoadFromStream(MemStream);
  44.         for I := 0 to Strings.Count - 1 do
  45.           WriteLn(Strings[I]);
  46.       finally
  47.         MemStream.Free;
  48.       end;
  49.     finally
  50.       Strings.Free;
  51.     end;
  52.   end;
  53.  
  54. begin
  55.   if (ParamCount = 0) or (ParamCount > 2) then
  56.     ShowSyntaxOptions
  57.   else
  58.     begin
  59.       Machine := UpperCase(ParamStr(1));
  60.       if ParamCount = 1 then
  61.         Option := 'QUERY'
  62.       else
  63.         Option := UpperCase(ParamStr(2));
  64.       StrPCopy(MachineBuffer,'\\'+Machine+'\Pipe\Service2b');
  65.       StrPCopy(CommandBuffer,Option);
  66.       FillChar(DataBuffer,Sizeof(DataBuffer),0);
  67.       if CallNamedPipe(@MachineBuffer,@CommandBuffer,StrLen(CommandBuffer) + 1,
  68.                        @DataBuffer,BuffSize,DataBytes,NMPWAIT_WAIT_FOREVER) then
  69.         ShowReturnedData
  70.       else
  71.         begin
  72.           WriteLn('Call to DemoService2b failed:-');
  73.           WriteLn(Format('   ''%s''',[SysErrorMessage(GetLastError)]));
  74.         end;
  75.     end;
  76. end.
  77.  
  78.